<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'配送记录'),
	array('op'=>'listday','title'=>'日配送统计'),
	array('op'=>'listmonth','title'=>'月配送统计'),
);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_peimilk_sendlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$member=pdo_get('jy_peimilk_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
		$item['username']=$member['title'];
		$store=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']));
		$item['merchname']=$store['merchname'];
		//$order=pdo_get('jy_weishop_order',array('id'=>$item['order_id'],'uniacid'=>$_SESSION['uniacid']));
		//$item['order']=$order;
		$item['goodsinfo']='';
		$item['goods']=json_decode($item['goods'],true);
		foreach($item['goods'] as &$gv){
				$gv['title']=cfc('tools')->substr($gv['title'],0,6);
				$item['goodsinfo'] .=$gv['title'];
				if($gv['selespename']){
					$item['goodsinfo'] .='['.$gv['selespename'].']';
				}
				if($gv['leititle']){
					$item['goodsinfo'] .='['.$gv['leititle'].']';
				}
				$item['goodsinfo'] .="(".$gv['num'].")；";
				$gv['unit']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'unit');
		}
		$item['member']=cfc('core')->member_show($item['buymid']);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'buymid','title'=>'客户ID','type'=>'string'),
			array('name'=>'member','title'=>'客户','type'=>'member'),
			array('name'=>'username','title'=>'配送员','type'=>'string'),
			array('name'=>'merchname','title'=>'所属商户','type'=>'string'),
			array('name'=>'day','title'=>'日期','type'=>'string'),
			array('name'=>'times','title'=>'时间','type'=>'string'),
			array('name'=>'goodsinfo','title'=>'商品详情','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'send_status'),
			
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;
	$return['send_status']=$this->send_status;
	exi($return);
}
if($_RQ['op']=='listday'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_peimilk_member',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$day=$_RQ['day']?strtotime($_RQ['day']):mktime(0,0,0,date('m'),date('d'),date('Y'));
	$return['params']['day']=date('Y-m-d',$day);
	$starttime=$day;
	$endtime=$starttime+24*3600;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	foreach($list as &$v){
		$params['mid']=$v['mid'];
		$params['createtime between']=array($starttime,$endtime);
		$v['num']=pdo_count('jy_peimilk_sendlog',$params,'id');
		$v['num']=$v['num']?:0;
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'title','title'=>'配送员','type'=>'string'),
				array('name'=>'num','title'=>'配送次数','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'配送员','type'=>'string'),
			array('name'=>'day','width'=>10,'title'=>'日期','type'=>'time'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_peimilk_member',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	$return['params']['y']=$y;
	$return['params']['m']=$m;
	$starttime=strtotime($y.'-'.$m);
	$endtime=strtotime($y.'-'.$m+1);
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	foreach($list as &$v){
		$params['mid']=$v['mid'];
		$params['createtime between']=array($starttime,$endtime);
		$v['num']=pdo_count('jy_peimilk_sendlog',$params,'id');
		$v['num']=$v['num']?:0;
	}
	$return['list'] =$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'title','title'=>'配送员','type'=>'string'),
				array('name'=>'num','title'=>'配送次数','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'配送员','type'=>'string'),
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'01'=>'1月',
		'02'=>'2月',
		'03'=>'3月',
		'04'=>'4月',
		'05'=>'5月',
		'06'=>'6月',
		'07'=>'7月',
		'08'=>'8月',
		'09'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_peimilk_sendlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}